package com.bishe.demo.controller;

import com.bishe.demo.pojo.Commodity;
import com.bishe.demo.pojo.Sale;
import com.bishe.demo.pojo.Vip;
import com.bishe.demo.pojo.vo.Status;
import com.bishe.demo.service.CommodityService;
import com.bishe.demo.service.SaleService;
import com.bishe.demo.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SaleController {

    @Autowired
    SaleService saleService;

    @Autowired
    CommodityService commodityService;

    @Autowired
    VipService vipService;

    @RequestMapping("tiaozhuanAddSale")
    public String tiaozhuanAddSale(HttpSession httpSession){

        List<Commodity> commodityList = commodityService.selectAllCom();
        List<Vip> vipList = vipService.selectVip();

        httpSession.setAttribute("comL",commodityList);
        httpSession.setAttribute("vipId",vipList);

        return "addSale";
    }
    //接收提交的销售信息

    @RequestMapping("receiveSale")
    public String receiveSale(Integer vipId,String commodityName, Integer commodityNumber, String commodityCategory, HttpSession httpSession){

        System.out.println("-------controllerzhong"+vipId);
        System.out.println("---"+commodityCategory);
        Status status = saleService.addSale(vipId,commodityName,commodityNumber,commodityCategory);
        httpSession.setAttribute("saleMsg",status.getMsg());

       return "addSaleMsg";
    }

    @RequestMapping("tiaozhuanSelectSale")
    public String tiaozhuanSelectSale(HttpSession httpSession){
        List<Sale> list = saleService.selectSale();
        int sum = 0 ;
        int saleNum = 0;
        for (Sale s: list) {
            saleNum = saleNum+s.getCommodityNumber()*s.getCommodityPrice();
            sum = sum+s.getCommodityNumber()*(s.getCommodityPrice()-s.getCommodityCost());
        }
        httpSession.setAttribute("saleNum",saleNum);
        httpSession.setAttribute("lirun",sum);
        httpSession.setAttribute("listSale",list);
        return "selectSale";
    }

    @RequestMapping("tiaozhuanDeleteSale")
    public String tiaozhuanDeleteSale(HttpSession httpSession){
        List<Sale> list = saleService.selectSale();
        httpSession.setAttribute("deleteSale",list);
        return "deleteSale";
    }
    //接收要删除的订单编号
    @RequestMapping("receiveDeleteSale")
    public String receiveDeleteSale(Integer saleId,HttpSession httpSession){

        Status status = saleService.deleteSale(saleId);
        httpSession.setAttribute("deleteMsg",status.getMsg());

        return "deleteSaleMsg";
    }
}
